/**
 * Copyright 2009 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.sapien.dao.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.google.code.sapien.dao.PersistentLoginDAO;
import com.google.code.sapien.model.PersistentLogin;
import com.google.code.sapien.model.User;
import com.google.inject.Inject;

/**
 * A JPA implementation of the {@link com.google.code.sapien.dao.PersistentLoginDAO} interface.
 * @author Adam
 * @version $Id$
 * 
 * Created on May 7, 2009 at 10:28:38 PM 
 */
public class PersistentLoginJpaDAO extends AbstractJpaDAO<PersistentLogin> implements PersistentLoginDAO {
	/**
	 * Constructs a PersistentLogin JPA data access object.
	 * @param entityManager The entity manager.
	 */
	@Inject
	public PersistentLoginJpaDAO(final EntityManager entityManager) {
		super(entityManager);
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.dao.PersistentLoginDAO#get(com.google.code.sapien.model.User, java.lang.String)
	 */
	public PersistentLogin get(final User user, final String token) {
		final Query query = getEntityManager().createNamedQuery("persistentLogin.getUserToken");
		query.setParameter("user", user);
		query.setParameter("token", token);
		return (PersistentLogin) query.getSingleResult();
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.dao.PersistentLoginDAO#list(com.google.code.sapien.model.User)
	 */
	@SuppressWarnings("unchecked")
	public List<PersistentLogin> list(final User user) {
		final Query query = getEntityManager().createNamedQuery("persistentLogin.listUser");
		query.setParameter("user", user);
		return query.getResultList();
	}

}
